package com.chen.librarysystem.menu;

import com.chen.librarysystem.context.AppContext;
import com.chen.librarysystem.entry.Book;
import com.chen.librarysystem.log.LibraryLog;
import com.chen.librarysystem.service.BookService;
import com.chen.librarysystem.service.impl.BookServiceImpl;
import com.chen.librarysystem.util.PrintUtils;

import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;

public class AdminBookMenu implements Menu {
    private static final Scanner sc = AppContext.getInstance().getScanner();
    private static final BookService bookService = new BookServiceImpl();

    @Override
    public void start() {
        while (true) {
            System.out.println("=========欢迎来到图书系统管理员界面==========");
            System.out.println("1. 添加图书");
            System.out.println("2. 删除图书");
            System.out.println("3. 修改图书");
            System.out.println("4. 根据图书名称或者作者名查找图书");
            System.out.println("5. 显示所有图书");
            System.out.println("6. 显示所有被借阅图书");
            System.out.println("7. 图书系统用户管理(管理员)");
            System.out.println("8. 退出图书系统管理员界面");
            int type = PrintUtils.getIntInput(sc, "请输入您的选择:");
            switch (type) {
                case 1:
                    LibraryLog.log(Level.INFO, "添加图书");
                    addBook();
                    break;
                case 2:
                    LibraryLog.log(Level.INFO, "删除图书");
                    removeBook();
                    break;
                case 3:
                    LibraryLog.log(Level.INFO, "修改图书");
                    updateBook();
                    break;
                case 4:
                    LibraryLog.log(Level.INFO, "根据图书名称或者作者名查找图书");
                    getBookByTitleOrAuthor();
                    break;
                case 5:
                    LibraryLog.log(Level.INFO, "显示所有图书");
                    getAllBooks();
                    break;
                case 6:
                    LibraryLog.log(Level.INFO, "显示所有被借阅图书");
                    getAllBorrowedBooks();
                    break;
                case 7:
                    LibraryLog.log(Level.INFO, "图书系统用户管理界面(管理员)");
                    new AdminUserManagementMenu().start();
                    break;
                case 8:
                    LibraryLog.log(Level.INFO, "退出图书系统管理员界面");
                    return;
                default:
                    System.out.println("输入错误，请重新输入");
            }
        }
    }

    private void getAllBorrowedBooks() {
        List<Book> allBorrowedBooks = AdminBookMenu.bookService.getAllBorrowedBooks();
        if (allBorrowedBooks.isEmpty()) {
            System.out.println("没有被借阅的图书");
            LibraryLog.log(Level.WARNING, "没有被借阅的图书");
            return;
        }
        PrintUtils.printBooks(allBorrowedBooks);
    }

    private void getAllBooks() {
        List<Book> allBooks = AdminBookMenu.bookService.getAllBooks();
        if (allBooks.isEmpty()) {
            LibraryLog.log(Level.INFO, "暂无数据");
            System.out.println("暂无数据");
            return;
        }
        PrintUtils.printBooks(AdminBookMenu.bookService.getAllBooks());
        LibraryLog.log(Level.INFO, "获取所有图书成功！");
    }

    private void getBookByTitleOrAuthor() {
        String titleOrAuthor = PrintUtils.getInput("请输入书名或作者");
        List<Book> books = AdminBookMenu.bookService.getBookByTitleOrAuthor(titleOrAuthor);
        if (books.isEmpty()) {
            LibraryLog.log(Level.WARNING, "没有找到图书");
            System.out.println("没有找到图书");
            return;
        }
        PrintUtils.printBooks(books);
        LibraryLog.log(Level.INFO, "共找到图书：" + books.size());
    }

    private void updateBook() {
        String title = PrintUtils.getInput("请输入要更新的图书名称:");
        String[] info = PrintUtils.getInput("请输入要更新作者名称，IBN号:").split("[,，]");
        LibraryLog.log(Level.INFO, "请依次输入图书作者，ISBN号，使用逗号分开：" + info[0] + "," + info[1]);
        Book book = new Book(title, info[0], info[1], false, null, null, null, null);
        if (AdminBookMenu.bookService.updateBook(title, book)) {
            System.out.println("修改成功!");
        } else {
            System.out.println("修改失败!");
        }

    }

    private void removeBook() {
        String title = PrintUtils.getInput("请输入要删除的图书名称：");
        if (AdminBookMenu.bookService.removeBookByTitle(title)) {
            System.out.println("删除成功！");
        } else {
            System.out.println("删除失败！");
        }
    }

    private void addBook() {
        String[] info = PrintUtils.getInput("请输入图书名称，作者名称，IBN号：").split("[,，]");
        LibraryLog.log(Level.INFO, "请依次输入图书名称，作者，ISBN号，使用逗号分开：" + info[0] + "," + info[1] + "," + info[2]);
        Book book = new Book(info[0], info[1], info[2], false, null, null, null, null);
        if (AdminBookMenu.bookService.addBook(book)) {
            System.out.println("添加成功!");
        } else {
            System.out.println("添加失败!书名已存在！");
        }
    }
}
